package com.zzyl.service.impl;

import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.date.LocalDateTimeUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.DeviceDataPageReqDto;
import com.zzyl.mapper.DeviceDataMapper;
import com.zzyl.service.DeviceDataService;
import com.zzyl.vo.DeviceDataGraphVo;
import com.zzyl.vo.DeviceDataVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * wangcong
 * 2025/2/13 10:20
 */
@Service
public class DeviceDataServiceImpl implements DeviceDataService {

    @Autowired
    DeviceDataMapper deviceDataMapper;

    @Override
    public PageResponse<DeviceDataVo> queryPage(DeviceDataPageReqDto deviceDataPageReqDto) {
        deviceDataPageReqDto.setStartDate(LocalDateTimeUtil.of(deviceDataPageReqDto.getStartTime()));
        deviceDataPageReqDto.setEndDate(LocalDateTimeUtil.of(deviceDataPageReqDto.getEndTime()));
        PageHelper.startPage(deviceDataPageReqDto.getPageNum(), deviceDataPageReqDto.getPageSize());
        Page<DeviceDataVo> page=deviceDataMapper.queryPage(deviceDataPageReqDto);
        return PageResponse.of(page , DeviceDataVo.class);
    }

   /* @Override
    public List<DeviceDataGraphVo> queryDeviceDataListByDay(String iotId, String functionId, Long startTime, Long endTime) {
        LocalDateTime start = LocalDateTimeUtil.of(startTime);
        LocalDateTime end = LocalDateTimeUtil.of(endTime);
        // 数据库中的数据
        List<DeviceDataGraphVo> list =deviceDataMapper.queryDeviceDataListByDay(iotId, functionId, start, end);

        // 所有数据
        List<DeviceDataGraphVo> dataList = DeviceDataGraphVo.dayInstance(LocalDateTimeUtil.of(startTime));
        Map<String, Double> map = list.stream().collect(Collectors.toMap(DeviceDataGraphVo::getDateTime, DeviceDataGraphVo::getDataValue));

        dataList.forEach(d->{
            Double val = map.get(d.getDateTime()) == null ? 0.0 : map.get(d.getDateTime());
            d.setDataValue(val);
        });

        return dataList;
    }*/
    @Override
    public List<DeviceDataGraphVo> queryDeviceDataListByDay(String iotId, String functionId, Long startTime, Long endTime) {
        LocalDateTime start = LocalDateTimeUtil.of(startTime);
        LocalDateTime end = LocalDateTimeUtil.of(endTime);
        // 数据库中的数据
        List<DeviceDataGraphVo> list =deviceDataMapper.queryDeviceDataListByDay(iotId, functionId, start, end);

        // 所有数据
        List<DeviceDataGraphVo> dataList = DeviceDataGraphVo.dayInstance(LocalDateTimeUtil.of(startTime));

        // 合并数据
       dataList.forEach(d->{
           list.forEach(l->{
               if (d.getDateTime().equals(l.getDateTime())){
                   if (l.getDataValue()!=null){
                       d.setDataValue(l.getDataValue());
                   }else {
                       d.setDataValue(0.0);
                   }
               }
           });
       });

        return dataList;
    }

    @Override
    public List<DeviceDataGraphVo> queryDeviceDataListByWeek(String iotId, String functionId, Long startTime, Long endTime) {
        LocalDateTime start = LocalDateTimeUtil.of(startTime);
        LocalDateTime end = LocalDateTimeUtil.of(endTime);
        // 数据库中的数据
        List<DeviceDataGraphVo> list =deviceDataMapper.queryDeviceDataListByWeek(iotId, functionId, start, end);

        // 所有数据
        List<DeviceDataGraphVo> dataList = DeviceDataGraphVo.weekInstance(start);
        Map<String, Double> map = list.stream().collect(Collectors.toMap(DeviceDataGraphVo::getDateTime, DeviceDataGraphVo::getDataValue));

        dataList.forEach(d->{
            Double val = map.get(d.getDateTime()) == null ? 0.0 : map.get(d.getDateTime());
            d.setDataValue(val);
        });

        return dataList;
    }
}
